﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NextCMS.Models;
using System.Xml.Linq;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class PageContents : IPageContent
    {
        private string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/Modules/PageContents.xml";
        XDocument doc = null;
        public PageContents()
        {
            doc = XDocument.Load(FilePath);  //load the xml file.
        }
        public PageContentModel GetById(int id,int LanguageId)
        {
            var q = from d in doc.Descendants("PageContent")
                    where (int)d.Element("Id") == id && (int)d.Element("LanguageId") == LanguageId
                    select new PageContentModel
                    {
                        Id = (int)d.Element("Id"),
                        LanguageId = (int)d.Element("LanguageId"),
                        Name = d.Element("Name").Value,
                        Content = d.Element("Content").Value,
                        IsGeneral = (bool)d.Element("IsGeneral")
                    };
            return q.FirstOrDefault();
        }
        public IEnumerable<PageContentModel> GetAll(int LanguageId)
        {
            var q = from d in doc.Descendants("PageContent")
                    where (int)d.Element("LanguageId") == LanguageId
                    select new PageContentModel
                    {
                        Id = (int)d.Element("Id"),
                        LanguageId = (int)d.Element("LanguageId"),
                        Name = d.Element("Name").Value,
                        Content = d.Element("Content").Value,
                        IsGeneral = (bool)d.Element("IsGeneral")
                    };
            return q;
        }
        public IEnumerable<PageContentModel> GetGeneralContents(int LanguageId)
        {
            var q = from d in doc.Descendants("PageContent")
                    where (int)d.Element("LanguageId") == LanguageId
                    select new PageContentModel
                    {
                        Id = (int)d.Element("Id"),
                        LanguageId = (int)d.Element("LanguageId"),
                        Name = d.Element("Name").Value,
                        Content = d.Element("Content").Value,
                        IsGeneral = (bool)d.Element("IsGeneral")
                    };
            return q.Where(c => c.IsGeneral == true);
        }
        public int Save(PageContentModel page)
        {
            if (page.Id == 0)
            {
                var q = from d in doc.Descendants("PageContent")
                        select new PageContentModel
                        {
                            Id = (int)d.Element("Id"),

                        };



                if (q.Count() > 0)
                    page.Id = q.Max(s => s.Id) + 1;
                else
                    page.Id = 1;
            }
            var oMember = new XElement("PageContent",
             new XElement("Id", page.Id),
             new XElement("LanguageId", page.LanguageId),
             new XElement("Name", String.IsNullOrEmpty(page.Name) ? "Content " + page.Id : page.Name),
             new XElement("Content", page.Content),
             new XElement("IsGeneral", page.IsGeneral)
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {
            doc.Root.Elements("PageContent").Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(PageContentModel page)
        {
            XElement node = doc.Root.Elements("PageContent").Where(i => (int)i.Element("Id") == page.Id && (int)i.Element("LanguageId") == page.LanguageId).FirstOrDefault();
            node.SetElementValue("LanguageId", page.LanguageId);
            node.SetElementValue("Name", page.Name);
            node.SetElementValue("Content", page.Content);
            node.SetElementValue("IsGeneral", page.IsGeneral);
            doc.Save(FilePath);
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
